<?php

	class StoryModel extends Model{

		protected $tableName = 'story';

		// 进度定义
		protected $progresses= array(
				0=>array(
					'story_status'=>'提测',
					'story_color'=>'danger',
					),
				1=>array(
					'story_status'=>'交互',
					'story_color'=>'warning',
					),
				2=>array(
					'story_status'=>'内测',
					'story_color'=>'active',
					),
				3=>array(
					'story_status'=>'外测',
					'story_color'=>'info',
					),
				4=>array(
					'story_status'=>'待发布',
					'story_color'=>'success',
					),
				5=>array(
					'story_status'=>'已上线',
					'story_color'=>'success',
					),
				6=>array(
					'story_status'=>'中断',
					'story_color'=>'danger',
					),
				);

		// 保存数据
		public function addStory($data){
			return $this->data($data)->add();
		}

		// 获取进度
		public function getProgresses(){
			return $this->progresses;
		}

		// 删除
		public function delStory($condition){
			return $this->where($condition)->delete();
		}

		// 查询
		public function getStories($condition,$start,$size){
			$stories=[];
			$stories = $this->where($condition)->order('story_inline_status,id desc')->limit($start,$size)->select();

			foreach ($stories as $key => &$story) {
				// 生成禅道链接
				$is_story='story';
				$story_mark=1;
				if(stripos($story['story_title'],'BUG')!==false){
					$is_story='bug';
					$story_mark=2;
				}
				
				$story['sendo_url']= C('zendo').'m='.$is_story.'&f=view&t=html&id='.$story['story_id'];

				//生成部署链接
				$bid=90;
				switch ($story['story_qat_tester']) {
					case '郑丹丹':
						$bid=181;
						break;
					case '潘陶华':
						$bid=188;
						break;					
					default:
						$bid=90;
						break;
				}
				$story['code_deploy_url']=C('code_deploy').'checkoutstoryall?bid='.$bid.'&storyid='.$story['story_id'].'&storytype='.$story_mark;
				$story['code_deploy_env']=C('code_deploy').'branch?id='.$bid;

	    		// 处理进度显示
	    		foreach ($this->progresses as $key => $story_progress) {
	    			if($story['story_inline_status']==$key){
	    				$story['story_status']=$story_progress['story_status'];
	    				$story['story_color']=$story_progress['story_color'];
	    				break;
	    			}
	    		}
	    		$story['story_has_bug'] = in_array($story['story_bug_type'],array(0,1))?'有':'无';
    		}
			return $stories;
		}

		// 需求总数
		public function getCounts($condition){
			return $this->where($condition)->count();
		}

		public function saveStory($condition,$data){
			return $this->where($condition)->save($data);
		}
	}